package com.ms.education.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ms.education.constant.RoleConstant;
import com.ms.education.constant.UcenterConstant;
import com.ms.education.ucenter.entity.Teacher;
import com.ms.education.ucenter.entity.TeacherApplication;
import com.ms.education.ucenter.entity.User;
import com.ms.education.ucenter.mapper.TeacherApplicationMapper;
import com.ms.education.ucenter.service.TeacherApplicationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ms.education.ucenter.service.TeacherService;
import com.ms.education.ucenter.service.UserService;
import com.ms.education.ucenter.service.feign.AclFeignService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ms
 * @since 2023-02-28
 */
@Service
public class TeacherApplicationServiceImp extends ServiceImpl<TeacherApplicationMapper, TeacherApplication> implements TeacherApplicationService {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private UserService userService;

    @Autowired
    private AclFeignService aclFeignService;

    @Override
    public TeacherApplication getUserApply(Integer userId) {
        QueryWrapper<TeacherApplication> wrapper = new QueryWrapper<TeacherApplication>().eq("user_id", userId);
        return getOne(wrapper);
    }

    @Override
    public void applyForTeacher(TeacherApplication teacherApplication) {
        TeacherApplication apply = getUserApply(teacherApplication.getUserId());
        if (apply != null) {
            BeanUtils.copyProperties(teacherApplication, apply);
            apply.setStatus(UcenterConstant.TeacherApplyEnum.STATUS_DEFAULT.getStatus());
            updateById(apply);
        } else {
            teacherApplication.setStatus(UcenterConstant.TeacherApplyEnum.STATUS_DEFAULT.getStatus());
            save(teacherApplication);
        }
    }

    @Override
    public Page<TeacherApplication> pageUserApply(long current, long limit, TeacherApplication teacherApplication) {
        Page<TeacherApplication> page = new Page<>(current, limit);
        QueryWrapper<TeacherApplication> wrapper = new QueryWrapper<>();
        String name = teacherApplication.getName();
        Integer status = teacherApplication.getStatus();
        Date beginTime = teacherApplication.getBeginTime();
        Date endTime = teacherApplication.getEndTime();
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }
        if (!StringUtils.isEmpty(beginTime)) {
            wrapper.ge("gmt_modified", beginTime);
        }
        if (!StringUtils.isEmpty(endTime)) {
            wrapper.ge("gmt_modified", endTime);
        }
        wrapper.orderByDesc("gmt_modified");
        page(page, wrapper);
        return page;
    }

    @Transactional
    @Override
    public void successApply(Integer id) {
        TeacherApplication teacherApplication = new TeacherApplication();
        teacherApplication.setId(id);
        teacherApplication.setStatus(UcenterConstant.TeacherApplyEnum.STATUS_YES.getStatus());
        updateById(teacherApplication);
        //讲师表新添加数据
        teacherApplication = getById(id);
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacherApplication, teacher);
        teacher.setId(null);
        teacherService.save(teacher);
        //修改用户表
        User user = new User();
        user.setId(teacherApplication.getUserId());
        user.setTeacherId(teacher.getId());
        userService.updateById(user);
        //为用户赋予讲师角色
        aclFeignService.endowedUserRole(teacherApplication.getUserId(), new Integer[]{RoleConstant.TEACHER_ROLE_ID});

    }

    @Override
    public void failApply(Integer id) {
        TeacherApplication teacherApplication = new TeacherApplication();
        teacherApplication.setId(id);
        teacherApplication.setStatus(UcenterConstant.TeacherApplyEnum.STATUS_NO.getStatus());
        updateById(teacherApplication);
    }
}
